import os
import sys

import pytest
from _pytest import logging

from utils.send_feishu_msg import send_feishu_message

# if __name__ == '__main__': # 用于运行环境检测
# __name__ 是一个内置变量，当文件运行时：__name__ 被设置为 __main__
# 当被导入到其他文件时，则判断条件不成立

# --alluredir ./report/json.report 指定一个目录，并生成中间结果
# --clean-alluredir # 每次运行会清空旧文件
if __name__ == '__main__': # 用于运行环境检测

    if len(sys.argv) == 2:
        if sys.argv[1] == "test":
            os.environ["URL"] = "http://192.168.10.142:80/api/cloud-api"
            os.environ["DB"] = str({
                "host": "192.168.10.142",
                "port": 3306,
                "database": "xingyun",
                "user": "root",
                "password": "123456",
            })
            os.environ["ENV"] = "test"
        elif sys.argv[1] == "prod":
            os.environ["URL"] = "http://192.168.10.142:80/api/cloud-api"
            os.environ["DB"]=str({
                "host":"192.168.10.142",
                "port":3306,
                "database":"xingyun",
                "user":"root",
                "password":"123456",
            })
            os.environ["ENV"] = "prod"
        else:
            logging.error("请传入正确的执行命令: python run.py test 或者 python run.py prod")
    else:
        logging.error("请传入正确的执行命令: python run.py test 或者 python run.py prod")

    pytest.main(["-vs","./testcases/test_runner.py","--alluredir","./report/json.report","--clean-alluredir"])
    os.system("allure generate ./report/json.report -o ./report/html.report --clean")

    # 发送测试报告消息
    send_feishu_message()